package com.netty.example.nio;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOTest3 {
    public static void main(String[] args) throws Exception {
        FileOutputStream fileOutputStream = new FileOutputStream("NIOTest3.txt");
        FileChannel channel = fileOutputStream.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(512);
        byte[] message = "hello world welcome".getBytes();
        for (int i = 0; i < message.length; i++) {
            buffer.put(message[i]);
        }
        buffer.flip();
        // buffer 中的数据写入 channel 中
        channel.write(buffer);
        System.out.println("write success");
        channel.close();
        fileOutputStream.close();
    }
}
